import os, sys, time, asyncio, aiohttp

import re
import tqdm

# 获取链接,下载文件
async def fetch(session: aiohttp.ClientSession, url: str, path: str, flag: str):
    print(flag, ' 开始下载')
    async with session.get(url) as resp:
        html = await resp.text()
        typelocation = re.search(r'安卓软件下载网</a> → <a href="/game/">安卓游戏</a>', html)
        if typelocation != None:
            apkUrlList = re.findall('href="(http.*?\.apk)"', html)
            apkSizeList = re.findall('(\d+\.\d+)MB', html)
            appName = re.findall('<div class="a_app_attr_tit">\n<h1>(.*?)</h1>', html)
            path = "E:/app/apk/%s.apk" % (appName[0] if appName.__len__() > 0 else str(0))
            if apkSizeList.__len__() > 0:
                if apkUrlList.__len__() > 0 and (float(apkSizeList[0])) < 100.0:
                    print('正在下载', apkUrlList[0])
                    async with session.get(apkUrlList[0]) as resp:
                        with open(path, 'wb') as fd:
                            while 1:
                                chunk = await resp.content.read(1024)  # 每次获取1024字节
                                if not chunk:
                                    break
                                fd.write(chunk)
    return flag


async def begin_download(sem, session: aiohttp.ClientSession, url: str, path: str, flag: str):
    # 控制协程并发数量
    with (await sem):
        return await fetch(session, url, path, flag)


async def download(sem: asyncio.Semaphore,path,url):
    async with aiohttp.ClientSession() as session:
        # 构造一个协程列表
        tasks.append(asyncio.ensure_future(begin_download(sem, session, url, path, path)))
        # 等待返回结果
        tasks_iter = asyncio.as_completed(tasks)
        # 创建一个进度条
        fk_task_iter = tqdm.tqdm(tasks_iter)
        for coroutine in fk_task_iter:
            # 获取结果
            res = await coroutine
            print(res, '下载完成')


# 创建目录
os.makedirs(DEST_DIR, exist_ok=True)
# 获取事件循环
lp = asyncio.get_event_loop()
tasks = []
# 创建一个信号量以防止DDos
sem = asyncio.Semaphore(4)
lp.run_until_complete(download(sem))
lp.close()
